<!--
 * @Description: 前缀列表
 * @Author: 0001101008/HuYuxi
 * @Date: 2021-11-02 09:01:24
 * @LastEditTime: 2021-11-02 15:29:32
 * @LastEditors: 0001101008/HuYuxi
-->
<template>
  <div class="page-content" style="padding: 0">
    <template v-if="show === 0">
      <div class="table-top">
        <!-- 刷新 -->
        <el-button size="medium" plain @click="refresh">刷新</el-button>
        <!-- 新建 -->
        <el-button size="medium" type="primary" plain @click="handleCreate" v-power="2">新建</el-button>
      </div>
      <tao-table
        class="table-box"
        :data="dataList"
        :page="page"
        @changePage="changePage"
        :showPage="true"
        ref="table"
      >
        <el-table-column label="详情" align="center" width="100" type="expand">
          <template slot-scope="scope">
            <info :row="scope.row"></info>
          </template>
        </el-table-column>
        <el-table-column label="ID" align="center" width="100" sortable prop="id" />
        <el-table-column label="前缀名称" min-width="200" prop="name" />
        <!-- 操作 -->
        <el-table-column fixed="right" align="center" label="操作" width="80px">
          <!--  删除-->
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: #FA6962"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-power="ne.dgId === 100000 ? 0 : 2"
            >删除</el-button>
          </template>
        </el-table-column>
      </tao-table>
    </template>
    <template v-if="show === 1">
      <forms :ne="ne" :type="type" @handleDiag="handleDiag"></forms>
    </template>
  </div>
</template>
<script>
import forms from './forms.vue';
import info from './info.vue';
export default {
  props: ['ne'],
  components: {
    forms,
    info
  },
  data () {
    return {
      dataList: [],
      page: {
        page: 1,
        pageSize: 15,
        total: 0
      },
      type: 'create',
      show: 0
    };
  },
  mounted () {
    let _this = this;
    _this.$nextTick(() => {
      _this.refresh();
    });
  },
  methods: {
    // 切换页码 & 显示条数
    changePage (page) {
      if (page.page) {
        this.page.page = page.page;
      } else {
        this.page.page = 1;
        this.page.pageSize = page.limit;
      }
      this.refresh();
    },
    // 新建
    handleCreate () {
      let _this = this;
      _this.type = 'create';
      _this.show = 1;
    },
    // 新建、编辑的回调
    handleDiag (data) {
      let _this = this;
      _this.type = 'create';
      _this.show = data;
    },
    // 刷新
    refresh () {
      let _this = this;
      let params = {
        neId: _this.ne.id,
        limit: _this.page.pageSize,
        offset: (_this.page.page - 1) * _this.page.pageSize
      };
      _this.$http.post({
        url: '/api/sdwan/prefixlist/query',
        data: params
      }).then(res => {
        let { status, resultBody } = res;
        if (status === '0') {
          _this.page.total = resultBody.count;
          _this.dataList = resultBody.objects;
        }
      });
    },
    // 删除
    handleDelete (row) {
      let _this = this;
      let params = {
        neId: row.neId,
        id: row.id,
        siteId: row.siteId,
        dgId: row.dgId,
        name: row.name,
        items: row.items
      };
      this.$confirm('您确定要删除当前数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        _this.$http.deleted({
          url: '/api/sdwan/prefixlist',
          data: params
        }).then(res => {
          let { status } = res;
          if (status === '0') {
            _this.$message.success('删除成功！');
            _this.refresh();
          }
        });
      }).catch(() => {
        _this.$message.info('取消删除');
      });
    }
  }
};
</script>
<style lang='scss' scoped>
.page-content {
  width: 100%;
  height: 100%;
  .table-top {
    margin-bottom: 15px;
  }
  .table-box {
    ::v-deep .el-button {
      padding: 0;
    }
    ::v-deep .el-dropdown-link {
      cursor: pointer;
      color: #409eff;
    }
  }
}
</style>